<!doctype html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../assert_selection.js"></script>
<script>
test(() => {
  assert_own_property(window, 'testRunner', 'This test requires testRunner');

  assert_selection(
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''),
  (selection, testRunner) => {
    let cutFired = false;
    selection.document.body.addEventListener('cut', () => cutFired = true);

    const link = selection.document.querySelector('a');
    link.focus();
    testRunner.execCommand('cut'); // Shouldn't cut 'text'
    assert_equals(selection.document.activeElement, link);
    assert_true(cutFired, "'cut' should still be dispatched even when cut command is disabled");
  },
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''));
}, 'Cutting with unfocused selection in contenteditable div');

test(() => {
  assert_own_property(window, 'testRunner', 'This test requires testRunner');

  assert_selection(
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>',
    '<div contenteditable id="target"></div>'
  ].join(''),
  (selection, testRunner) => {
    selection.setClipboardData('foo');

    let copyFired = false;
    selection.document.body.addEventListener('copy', () => copyFired = true);

    const link = selection.document.querySelector('a');
    link.focus();
    testRunner.execCommand('copy'); // Shouldn still copy 'text'
    const target = selection.document.getElementById('target');
    target.focus();
    testRunner.execCommand('paste');
    assert_true(copyFired, "'copy' should still be dispatched even when copy command is disabled");
  },
  [
    '<div contenteditable>text</div>',
    '<a href="http://www.example.com/">link</a>',
    '<div contenteditable id="target">text|</div>'
  ].join(''));
}, 'Copying with unfocused selection in contenteditable div');

test(() => {
  assert_own_property(window, 'testRunner', 'This test requires testRunner');

  assert_selection(
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''),
  (selection, testRunner) => {
    selection.setClipboardData('foo');

    let pasteFired = false;
    selection.document.body.addEventListener('paste', () => pasteFired = true);

    const link = selection.document.querySelector('a');
    link.focus();
    testRunner.execCommand('paste'); // Shouldn't paste
    assert_equals(selection.document.activeElement, link);
    assert_true(pasteFired, "'paste' should still be dispatched even when paste command is disabled");
  },
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''));
}, 'Pasting with unfocused selection in contenteditable div');

test(() => {
  assert_own_property(window, 'testRunner', 'This test requires testRunner');

  assert_selection(
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''),
  (selection, testRunner) => {
    selection.setClipboardData('foo');

    let pasteFired = false;
    selection.document.body.addEventListener('paste', () => pasteFired = true);

    const link = selection.document.querySelector('a');
    link.focus();
    testRunner.execCommand('pasteAndMatchStyle'); // Shouldn't paste
    assert_equals(selection.document.activeElement, link);
    assert_true(pasteFired, "'paste' should still be dispatched even when paste command is disabled");
  },
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''));
}, 'Pasting as plain text with unfocused selection in contenteditable div');

test(() => {
  assert_own_property(window, 'testRunner', 'This test requires testRunner');
  assert_own_property(window, 'internals', 'This test requires internal settings');
  internals.settings.setEditingBehavior('unix');

  assert_selection(
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''),
  (selection, testRunner) => {
    selection.setClipboardData('foo');

    let pasteFired = false;
    selection.document.body.addEventListener('paste', () => pasteFired = true);

    const link = selection.document.querySelector('a');
    link.focus();
    testRunner.execCommand('pasteGlobalSelection'); // Shouldn't paste
    assert_equals(selection.document.activeElement, link);
    assert_true(pasteFired, "'paste' should still be dispatched even when paste command is disabled");
  },
  [
    '<div contenteditable>^text|</div>',
    '<a href="http://www.example.com/">link</a>'
  ].join(''));
}, 'Pasting global selection with unfocused selection in contenteditable div');
</script>
